package monsterRun.client.model;

import java.awt.KeyboardFocusManager;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import Testing.PlayerCallBackImpl;
import Testing.PlayerCallBackInterface;
import Testing.PlayerMessagesInterface;
import Testing.Server.RMI_Server;

import monsterRun.client.controller.GameKeyListener;
import monsterRun.client.model.events.IThemeChanged;
import monsterRun.client.view.MonsterFrame;
import monsterRun.client.view.renderer.themes.AbstractBoardTheme;
import monsterRun.client.view.renderer.themes.DefaultBoardTheme;
import monsterRun.common.model.Constants;
import monsterRun.common.model.enums.GameBoardType;
import monsterRun.common.model.jevents.JEvent;
import monsterRun.common.model.network.Communicator;

public class Facade
{

	private GameBoard gameBoard;
	private GameKeyListener keyListener;

	// TODO For Ian: PlayerMessageInterface lives here
	private PlayerMessagesInterface pmi;

	private PlayerCallBackInterface pcbi;
	private AbstractBoardTheme theme;
	private int playerID = -1;
	private MonsterFrame frame;

	public final JEvent<IThemeChanged> themeChanged = JEvent
			.create(IThemeChanged.class);

	public Facade(int playerNum) throws MalformedURLException, RemoteException,
			NotBoundException
	{
		this.frame = frame;

		theme = new DefaultBoardTheme();

		// Connection
		//communicator = new Communicator(Constants.HOST, Constants.PORT, true);

		// Setting the player number here so that our server can differentiate
		// players
		playerID = playerNum;

		// Game Board
		gameBoard = new GameBoard(GameBoardType.DEFAULT, 
				this, pmi);

		// Key Listener
		keyListener = new GameKeyListener(Constants.KEY_DELAY);

		KeyboardFocusManager.getCurrentKeyboardFocusManager()
				.addKeyEventDispatcher(keyListener);

		enableKeyListener(false);
	}

	public GameBoard getGameBoard()
	{
		return gameBoard;
	}

	public GameKeyListener getKeyListener()
	{
		return keyListener;
	}


	public PlayerMessagesInterface getPlayerMessagesInterface()
	{
		return pmi;
	}

	public void setPmi(PlayerMessagesInterface pmi)
	{
		this.pmi = pmi;
	}

	public AbstractBoardTheme getTheme()
	{
		return theme;
	}

	public void setTheme(AbstractBoardTheme theme)
	{
		this.theme = theme;
		themeChanged.get().themeChanged(theme);
	}

	public void enableKeyListener(boolean enable)
	{
		keyListener.setEnabled(enable);
	}

	public void initializeGameBoard(GameBoardType board)
	{
		gameBoard.reInitialize(board);
	}

	

	public void disconnectFromServer() throws IOException
	{
	//	communicator.close();
	}

	public int getPlayerID()
	{
		return playerID;
	}

	public void setPlayerID(int playerID)
	{
		this.playerID = playerID;
	}

	public void setPcbi(PlayerCallBackInterface pcbi)
	{
		this.pcbi = pcbi;
	}
}
